package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.BaseDaoI;
import com.easy.common.dao.DBUtilsHelper;
import com.easy.common.exception.Exceptions;
import com.easy.common.util.BeanUtils;
import com.easy.common.util.number.RandomUtils;
import com.easy.common.web.page.Msg;
import com.easy.common.web.springmvc.RealPathResolver;
import com.easy.modules.sysmgr.entity.AreaEntity;
import com.easy.modules.sysmgr.service.AreaServiceI;
import com.easy.modules.sysmgr.web.form.AreaForm;

@Service @Transactional
public class AreaServiceImpl implements AreaServiceI {
	
	private static final Logger logger = Logger.getLogger(AreaServiceImpl.class);

	@Autowired
	private DBUtilsHelper db ;
	
	@Autowired
	private BaseDaoI<AreaEntity> basedaoArea ;
	
	@Autowired
	private RealPathResolver realpath ;
	
	@Override
	public Msg add(AreaForm form) {
		try {
			AreaEntity entity = new AreaEntity() ;
			BeanUtils.copyProperties(form, entity) ;
			
			entity.setId(new Long(RandomUtils.generateNumber(4))) ;
			entity.setParentAreaId(new Long(0)) ;
			entity.setAreaLevel(0) ;
			entity.setEnabled(true) ;
			if(null != form.getParentAreaId()) {
				entity.setParentAreaId(form.getParentAreaId()) ;
			}
			this.basedaoArea.save(entity) ;
			return new Msg(true, "添加成功!");
		} catch (BeansException e) {
			logger.error(Exceptions.unchecked(e)) ;
			return new Msg(false, "添加失败!");
		}
	}

	@Override
	public Msg delete(AreaForm form) {
		try {
			AreaEntity t = this.basedaoArea.get(AreaEntity.class, form.getId());
			del(t);
			
			return new Msg(true, "删除成功!");
		} catch (Exception e) {
			logger.error(Exceptions.unchecked(e)) ;
			return new Msg(false, "删除失败，该模块可能已分配给角色了，请先解除关联！");
		}
	}
	private void del(AreaEntity entity) {
		this.basedaoArea.delete(entity);
	}

	@Override
	public AreaForm get(AreaForm form) {
		AreaEntity entity = this.basedaoArea.get(AreaEntity.class, form.getId());
		BeanUtils.copyProperties(entity, form);
		return form;
	}
	
	@Override
	public Msg edit(AreaForm form) {
		try {
			AreaEntity entity = this.basedaoArea.get(AreaEntity.class, form.getId());
			BeanUtils.copyProperties(form, entity);
			
			entity.setParentAreaId(new Long(0)) ;
			entity.setAreaLevel(0) ;
			entity.setEnabled(true) ;
			if(null != form.getParentAreaId()) {
				entity.setParentAreaId(form.getParentAreaId()) ;
			}
			
			this.basedaoArea.update(entity);
			return new Msg(true, "编辑成功!");
		} catch (Exception e) {
			logger.error(Exceptions.unchecked(e)) ;
			return new Msg(false, "编辑失败！");
		}
	}

	@Override
	public List<AreaForm> findAreas(AreaForm form) {
		
		List<AreaForm> listform = new ArrayList<AreaForm>() ;
		
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from AreaEntity t where 1=1";
		List<AreaEntity> entitys = this.basedaoArea.find(addWhere(hql, form, params), params) ;
		for (AreaEntity entity : entitys) {
			AreaForm f = new AreaForm() ;
			f.setText(entity.getAreaName()) ;
			BeanUtils.copyProperties(entity, f) ;
			
			String childHql = "select count(*) from AreaEntity t where t.parentAreaId="+entity.getId() ;
			Long count = this.basedaoArea.count(childHql) ;
			if(count > 0) {
				f.setState("closed") ;
			}
			
			f.setIconCls("icon-hamburg-payment-card") ;
			listform.add(f) ;
		}
		return listform;
	}

	@Override
	public List<AreaForm> findProvince(AreaForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AreaForm> findCity(AreaForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AreaForm> findCounty(AreaForm form) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private String addWhere(String hql, AreaForm form, Map<String, Object> params) {
		if (null != form) {
			if(null != form && null != form.getId()) {
				hql += " and t.parentAreaId=:pid" ;
				params.put("pid", form.getId()) ;
			} else {
				hql += " and t.parentAreaId=:pid" ;
				params.put("pid", new Long(0)) ;
			}
		}
		return hql;
	}

}
